package net.entelijan.cobean.examples.showcase.multisel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.IMultiSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.multisel.DefaultMultiselInitializer;
import net.entelijan.cobean.util.PropertyComparator;
import net.entelijan.scf.IShowcase;

public class SortMultiselShowcase implements IShowcase<SortMultiselPanel> {

	private ILitService service = null;

	public SortMultiselShowcase() {
		super();
	}

	@Override
	public void init(final SortMultiselPanel panel) {
		// Create an initializer
		DefaultMultiselInitializer<LitBook> ini = new DefaultMultiselInitializer<LitBook>();

		// Add the required components to the initializer.
		ini.setCoAddButton(panel.getMultiselPanel().getAddButton());
		ini.setCoRemoveButton(panel.getMultiselPanel().getRemoveButton());
		ini.setCoLeftList(panel.getMultiselPanel().getLeftList());
		ini.setCoRightList(panel.getMultiselPanel().getRightList());

		// Define the colums that should be displayed in the lists
		ini.setColumnDescs(ColumnDesc.createPropertyOnlyColumnDescs("title",
				"subtitle", "year", "author.name"));

		ini.setSorted(true);
		ini.setComparator(new PropertyComparator<LitBook>("year", true));

		final IMultiSelectionListCobean<LitBook> cobean = ini.initialize();

		// Add some data to the component bean
		final List<LitBook> data = service.loadAllBooks();
		cobean.setValue(data);
		// Select the 4th Item at startup
		ArrayList<LitBook> selData = new ArrayList<LitBook>();
		selData.add(data.get(3));
		selData.add(data.get(4));
		selData.add(data.get(5));
		selData.add(data.get(6));
		cobean.setSelectedValue(selData);

		// Init the show selection button
		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, ExamplesUtil.listToString(
						cobean.getSelectedValue(), 80));
			}
		});

	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
